package vip.george.controller;

import cn.hutool.core.util.StrUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author gehaodong
 * @since 2024/3/11
 * <p>
 * </P>
 */

@RestController
public class LoginController {


    @GetMapping(value = "/doLogin", produces = "text/html;charset=utf-8")
    public String login(String username, String password) {
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return "用户名或密码不能为空！";
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            //登录成功后可以获取所有realm的认证信息
            List list = subject.getPrincipals().asList();
            for (Object principal : list) {
                System.out.println(principal.getClass() + "--->" + principal);
            }
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return "登陆失败！" + e.getMessage();
        }
        return "login success";
    }
}
